package com.david.appletree;

import android.app.Application;
import android.app.UiModeManager;
import android.content.Context;
import android.content.res.Configuration;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;
import com.tencent.smtt.sdk.QbSdk;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.able.Cancelable;
import com.yanzhenjie.nohttp.rest.AsyncRequestExecutor;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;

class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        lockMulticast();


        UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
        if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {

            Log.d("TAG", "Running on a TV Device");
        } else {

            Log.d("TAG", "Running on a non-TV Device");
        }

        NoHttp.initialize(this);


        StringRequest request = new StringRequest("http://api.nohttp.net");
        Cancelable cancel = AsyncRequestExecutor.INSTANCE.execute(0, request, new SimpleResponseListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                // 请求成功。
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                // 请求失败。
            }
        });

        ARouter.init(this);
    }

    private void lockMulticast() {
        WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifi == null)
            return;

        WifiManager.MulticastLock lock = wifi.createMulticastLock("ONVIF");
        lock.acquire();

        initX5app(this);


    }

    public void initX5app(Context context){
        //设置非wifi条件下允许下载X5内核
        QbSdk.setDownloadWithoutWifi(true);

        //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
            @Override
            public void onViewInitFinished(boolean arg0) {
                //x5內核初始化完成的回调，true表x5内核加载成功，否则表加载失败，会自动切换到系统内核。
                Log.d("app", " 内核加载 " + arg0);
            }
            @Override
            public void onCoreInitFinished() {
            }
        };

        //x5内核初始化接口
        QbSdk.initX5Environment(context,  cb);
    }
}
